package org.adobe.utils {

	/**
	 *  数组常用方法
	 */	
	public class ArrayUtil {
		public function ArrayUtil(){
			throw new Error("Array Util class is static container only")
		}

		/**数组中是否含有某一指定对象*/
		public static function arrayContainsValue(arr:Array, value:Object):Boolean {
			return (arr.indexOf(value) != -1);
		}

		/**删除数组的某一项*/
		public static function removeValueFromIndex(arr:Array, index:uint):void {
			/*delete arr[index]
			   for(var i:int = index; i<arr.length; i++){
			   arr[i] = arr[i+1]
			 }*/
			for (var i:int = index; i < arr.length - 1; i++){
				arr[i] = arr[i + 1]; //值往前排
			}
			delete arr[i];
			arr.length--;
		}

		/**从数组移除某一值*/
		public static function removeValueFromArray(arr:Array, value:Object):void {
			var len:uint = arr.length;
			for (var i:Number = len; i > -1; i--){
				if (arr[i] === value){ //removeValueFromIndex(arr,i)
					arr.splice(i, 1);
				}
			}
		}

		/**深度拷贝一个数组,重复值只保留一个*/
		public static function createUniqueCopy(a:Array):Array {
			var newArray:Array = new Array();
			var len:Number = a.length;
			var item:Object;
			for (var i:uint = 0; i < len; ++i){
				item = a[i];
				if (ArrayUtil.arrayContainsValue(newArray, item)){
					continue;
				}
				newArray.push(item);
			}
			return newArray;
		}

		/**拷贝一个数组，与原数组无关联*/
		public static function copyArray(arr:Array):Array {
			return arr.slice();
		}

		/**
		 * 两个数组作比较
		 * 两数组中的元素一致且在数组中的位置相同
		 * 若数组中出现对对象的引用，则对对象的引用也要求相同
		 */
		public static function arraysAreEqual(arr1:Array, arr2:Array):Boolean {
			if (arr1.length != arr2.length){
				return false;
			}
			var len:Number = arr1.length;
			for (var i:Number = 0; i < len; i++){
				if (arr1[i] !== arr2[i]){
					return false;
				}
			}
			return true;
		}
	}
}